package com.ruoyi.project.mgr.teacher.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.mgr.clazz.domain.MgrClazz;
import com.ruoyi.project.mgr.clazz.service.IMgrClazzService;
import com.ruoyi.project.mgr.course.domain.MgrCourse;
import com.ruoyi.project.mgr.course.service.IMgrCourseService;
import com.ruoyi.project.mgr.teacher.domain.MgrClazzTeacher;
import com.ruoyi.project.mgr.teacher.domain.MgrTeacher;
import com.ruoyi.project.mgr.teacher.service.IMgrTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/mgr/teacher")
public class MgrTeacherController extends BaseController {

    private String prefix = "mgr/teacher";

    @Autowired
    private IMgrClazzService mgrClazzService;

    @Autowired
    private IMgrTeacherService mgrTeacherService;

    @Autowired
    private IMgrCourseService mgrCourseService;

    @GetMapping()
    public String teacher(ModelMap modelMap)
    {
        modelMap.put("courses", mgrCourseService.selectMgrCourseList(new MgrCourse()));
        return prefix + "/teacher";
    }

    /**
     * 查询课程安排列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MgrTeacher mgrTeacher)
    {
        startPage();
        List<MgrTeacher> list = mgrTeacherService.selectTeacherList(mgrTeacher);
        return getDataTable(list);
    }

    /**
     * 查看班级
     * @param modelMap
     * @return
     */
    @GetMapping("/view/{teacherId}")
    public String view(@PathVariable("teacherId") Long teacherId, ModelMap modelMap)
    {
        modelMap.put("teacherId", teacherId);
        modelMap.put("clazzs", mgrClazzService.selectMgrClazzList(new MgrClazz()));
        return prefix + "/clazz";
    }

    @PostMapping("/clazzList")
    @ResponseBody
    public TableDataInfo clazzList(MgrClazzTeacher mgrClazzTeacher)
    {
        startPage();
        List<MgrClazz> list = mgrClazzService.selectClazzByTeacher(mgrClazzTeacher.getTeacherId());
        return getDataTable(list);
    }

    /**
     * 删除教师所带的班级
     * @param clazzId
     * @param teacherId
     * @return
     */
    @GetMapping("/delClazz/{clazzId}/{teacherId}")
    @ResponseBody
    public AjaxResult delClazz(@PathVariable("clazzId") Long clazzId,@PathVariable("teacherId") Long teacherId){
        return toAjax(mgrTeacherService.deleteClazz(clazzId, teacherId));
    }

    /**
     * 添加班级
     * @param teacherId
     * @param modelMap
     * @return
     */
    @GetMapping("/add/{teacherId}")
    public String addClazz(@PathVariable("teacherId") Long teacherId, ModelMap modelMap){
        modelMap.put("teacherId", teacherId);
        modelMap.put("clazzs", mgrClazzService.selectClazzNotExistByTeacher(teacherId));
        return prefix + "/add";
    }

    @PostMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave(MgrClazzTeacher mgrClazzTeacher){
        return toAjax(mgrTeacherService.insertMgrClazzTeacher(mgrClazzTeacher));
    }
}
